<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="bp" value="${pageContext.request.contextPath}"></c:set>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
 <link rel="stylesheet" href="${bp}/static/css/my.css">
</head>
<html>
<div style="margin-left: 300px;margin-top: 300px">
    <button  class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal2">新增用户</button>
    <div class="container">
        <h2>基础表格</h2>
        <p>.table 类来设置基础表格的样式:</p>
        <table class="table">
            <thead>
            <tr>
                <th>序号</th>
                <th>用户名</th>
                <th>部门</th>
                <th>编辑</th>
                <th>删除</th>
                <th>性别</th>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${staff}" var="keyword" varStatus="id">
                <tr>
                    <td>${keyword.lastname}</td>
                    <td>${keyword.email}</td>
                    <td>${keyword.department}</td>
                    <td>${keyword.edit}</td>
                    <td>${keyword.dalete}</td>
                    <td>${keyword.gender}</td>
                    <td>
                        <button onclick="deleteId(${keyword.id})">删除</button>
                        <button onclick="findAll(${keyword.id})"  data-toggle="modal" data-target="#myModal">更新</button>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
</div>



<!-- 模态框（更新） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    更新用户数据
                </h4>
            </div>
            <div class="modal-body">
                <label>序号：</label><input type="text" id="id"  value="" ><br>
                <label>名字：</label><input type="text" id="lastname"  value="" ><br>
                <label>邮件：</label><input type="text" id="email"  value="" ><br>
                <label>部门：</label><input type="text" id="department"  value="" ><br>
                <label>编辑：</label><input type="text" id="edit"  value="" ><br>
                <label>删除：</label><input type="text" id="dalete"  value="" ><br>
                <label>业绩：</label><input type="text" id="gender"  value="" ><br>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" onclick="updateid()" class="btn btn-primary">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- 模态框（添加） -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel2">
                    新增用户数据
                </h4>
            </div>
            <div class="modal-body">
                <label>名字：</label><input type="text" id="lastname1"  value="" ><br>
                <label>邮件：</label><input type="text" id="email1"  value="" ><br>
                <label>部门：</label><input type="text" id="department1"  value="" ><br>
                <label>编辑：</label><input type="text" id="edit1"  value="" ><br>
                <label>删除：</label><input type="text" id="dalete1"  value="" ><br>
                <label>业绩：</label><input type="text" id="gender1"  value="" ><br>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" onclick="add()" class="btn btn-primary">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->

    <!-- 模态框（添加） -->





        <!-- 模态框（添加） -->



        <div class="modal fade" id="myModal4" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            &times;
                        </button>
                        <h4 class="modal-title" id="myModalLabel4">
                            新增用户数据
                        </h4>
                    </div>
                    <div class="modal-body">
                        <label>名字：</label><input type="text" id="lastname2"  value="" ><br>
                        <label>邮件：</label><input type="text" id="email2"  value="" ><br>
                        <label>部门：</label><input type="text" id="department2"  value="" ><br>
                        <label>编辑：</label><input type="text" id="edit2"  value="" ><br>
                        <label>删除：</label><input type="text" id="dalete2"  value="" ><br>
                        <label>业绩：</label><input type="text" id="gender2"  value="" ><br>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                        </button>
                        <button type="button" onclick="findid()" class="btn btn-primary">
                            提交更改
                        </button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->


</div>
</div>
<script type="text/javascript">




    function  deleteId(id) {
        $.ajax({
            type: "GET",
            url: "${bp}/staff/deleteId?id="+ id,

            // 异步回调
            success: function (result) {
               if(result ==1){
                   alert("删除成功")
                   window.location.reload()
               }
            }
        });
    }

    function  findid(id) {
        $.ajax({
            type: "GET",
            url: "${bp}/staff/findid?id="+id,
            // 异步回调
            success: function (result) {
                var parsedJson = jQuery.parseJSON(result);
                var id = parsedJson.id
                var lastname= parsedJson.lastname
                var  email   = parsedJson.email
                var  department= parsedJson.department
                var  edit    = parsedJson.edit
                var  dalete  = parsedJson.dalete
                var  gender  = parsedJson.gender
                document.getElementById("id").value=id
                document.getElementById("lastname").value=lastname
                document.getElementById("email").value=email
                document.getElementById("department").value=department
                document.getElementById("edit").value=edit
                document.getElementById("dalete").value=dalete
                document.getElementById("gender").value=gender
            }
        });
    }

    function findAll () {
        $.ajax({
            type: "GET",
            url: "${bp}/staff/findAll?id="+id,
            // 异步回调
            success: function (result) {
                var lastname = $("#lastname1").val()
                var email = $("#email1").val()
                var department = $("#department1").val()
                var edit = $("#edit1").val()
                var dalete = $("#dalete1").val()
                var gender = $("#gender1").val()
                document.getElementById("id").value=id
                document.getElementById("lastname").value=lastname
                document.getElementById("email").value=email
                document.getElementById("department").value=department
                document.getElementById("edit").value=edit
                document.getElementById("dalete").value=dalete
                document.getElementById("gender").value=gender
                }

        });
    }


    function updateid() {
        var id = $("#id").val()
        var lastname = $("#lastname").val()
        var email = $("#email").val()
        var department = $("#department").val()
        var edit = $("#edit").val()
        var dalete = $("#dalete").val()
        var gender = $("#gender").val()
        $.ajax({
            type: "GET",
            url: "${bp}/staff/updateid",
            data:{"id":id,"lastname":lastname,"email": email, "department":department, "edit":edit, "dalete":dalete, "gender":gender},
            // 异步回调
            success: function (result) {
                if (result == 1){
                    alert("更新成功")
                    window.location.href="${bp}/staff/index"
                }else {
                    alert("更新失败")
                }
            }
        });
    }
        function add() {

            var id = $("#id").val()
            var lastname = $("#lastname").val()
            var email = $("#email").val()
            var department = $("#department").val()
            var edit = $("#edit").val()
            var dalete = $("#dalete").val()
            var gender = $("#gender").val()
            $.ajax({
                type: "post",
                url: "${bp}/staff/add",
                data:{"id":id,"lastname":lastname,"email": email, "department":department, "edit":edit, "dalete":dalete, "gender":gender},
                // 异步回调
                success: function (result) {
                    if (result == 1){
                        alert("添加成功")
                        window.location.href="${bp}/staff/index"
                    }else {
                        alert("更新失败")
                    }
                }
            });
    }

</script>


</html>